<template>
    <div>
        <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/sys-admin/index' }" style="size: 16px">
                <i class="el-icon-s-promotion"></i>
                后台管理
            </el-breadcrumb-item>
            <el-breadcrumb-item>添加属性</el-breadcrumb-item>
        </el-breadcrumb>

        <el-divider></el-divider>

        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="属性模板" prop="templateId">
                <template>
                    <el-select v-model="ruleForm.templateId" placeholder="请选择">
                        <el-option
                                v-for="item in templateArr"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id">
                        </el-option>
                    </el-select>
                </template>
            </el-form-item>
            <el-form-item label="名称" prop="name">
                <el-input v-model="ruleForm.name" ></el-input>
            </el-form-item>
            <el-form-item label="简介" prop="description">
                <el-input v-model="ruleForm.description" ></el-input>
            </el-form-item>
            <el-form-item label="类型" prop="type">
                    <template>
                        <el-radio v-model="ruleForm.type" label="0">非销售属性</el-radio>
                        <el-radio v-model="ruleForm.type" label="1">销售属性</el-radio>
                    </template>
            </el-form-item>
            <el-form-item label="输入类型" prop="inputType">
                <template>
                    <el-radio v-model="ruleForm.inputType" label="0">手动录入</el-radio>
                    <el-radio v-model="ruleForm.inputType" label="1">单选</el-radio>
                    <el-radio v-model="ruleForm.inputType" label="2">多选</el-radio>
                    <el-radio v-model="ruleForm.inputType" label="3">单选(下拉列表)</el-radio>
                    <el-radio v-model="ruleForm.inputType" label="4">多选(下拉列表)</el-radio>
                </template>
            </el-form-item>
            <el-form-item label="备选值列表" prop="valueList">
                <el-input v-model="ruleForm.valueList" ></el-input>
            </el-form-item>
            <el-form-item label="计量单位" prop="unit">
                <el-input v-model="ruleForm.unit" ></el-input>
            </el-form-item>
            <el-form-item label="排序序号" prop="sort">
                <el-input v-model="ruleForm.sort" ></el-input>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    export default {

        data() {
            return {
                templateArr:[],
                ruleForm: {
                    templateId:'',
                    name:'我的测试数据',
                    description:'',
                    type:'0',
                    inputType:'0',
                    valueList:'',
                    unit:'',
                    sort:'',
                },
                rules: {
                    templateId: [
                        { required: true, message: '请选择属性模板', trigger: 'blur' },
                        { pattern:/^(\d{1}|[1-9]{1}[0-9]?)$/, message: '排序序号必须是 0~99 之间的数字', trigger: 'blur' }
                    ],
                    name: [
                        { required: true, message: '请输入属性名称', trigger: 'blur' },
                        { min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur' }
                    ],
                    description: [
                        { required: false, message: '请输属性描述', trigger: 'blur' },
                        { min: 2, max: 35, message: '长度在 2 到 35 个字符', trigger: 'blur' }
                    ],
                    valueList: [
                        { required: false, message: '请输入备选值列表', trigger: 'blur' },
                        { min: 2, max: 35, message: '长度在 2 到 35 个字符', trigger: 'blur' }
                    ],
                    unit: [
                        { required: false, message: '请输入计量单位', trigger: 'blur' },
                        { min: 2, max: 35, message: '长度在 2 到 5 个字符', trigger: 'blur' }
                    ],
                    sort: [
                        { required: false, message: '请输入排序序号', trigger: 'blur' },
                        { pattern:/^(\d{1}|[1-9]{1}[0-9]?)$/, message: '排序序号必须是 0~99 之间的数字', trigger: 'blur' }
                    ]
                }
            };
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        let url="http://localhost:9080/attributes/add-new"
                        console.log('url='+url);

                        let fromData=this.qs.stringify(this.ruleForm);
                        console.log('fromData='+fromData);

                        this.axios
                            .create({
                                headers:{
                                    'Authorization':localStorage.getItem('jwt'),
                                }
                            })
                            .post(url,fromData).then((response)=>{
                            let responseBody=response.data;
                            console.log(responseBody);
                            if(responseBody.state==20000){
                                this.$message({
                                    showClose: true,
                                    message: '属性添加成功!',
                                    type: 'success'
                                });
                                this.resetForm(formName);
                            }else{
                                this.$message({
                                    showClose: true,
                                    message: responseBody.message,
                                    type: 'error'
                                });
                            }
                        });
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            loadAttributeTemplateList(){
                console.log("loadAttributeTemplateList....");
                let url='http://localhost:9080/attribute-template';
                console.log('url='+url);
                this.axios
                    .create({
                        headers:{
                            'Authorization':localStorage.getItem('jwt'),
                        }
                    })
                    .get(url).then((response)=>{
                    let responseBody=response.data;
                    if(responseBody.state==20000){
                        this.templateArr=responseBody.data;
                    }
                })
            }
        },
        mounted() {
            this.loadAttributeTemplateList();
        }
    }

</script>

<style scoped>

</style>